﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace DScheme.DancerClasses
{
    class DancerActionMoveByEllipseOn : DancerAction
    {
        public PointD posC;
        protected double radius1;
        protected double radius2;
        protected double startAngle;
        protected double moveAngle;
        protected bool backward = false;

        const bool allwaysForward = false;

        public DancerActionMoveByEllipseOn(Dancer dancer, double radius1, double radius2, double startAngle, double moveAngle, bool backward, double startTime, double moveTime)
            : base(dancer, startTime, moveTime)
        {
            this.radius1 = radius1;
            this.radius2 = radius2;
            this.startAngle = startAngle;
            this.moveAngle = moveAngle;
            this.backward = backward;
        }

        protected PointD GetPosCenter(PointD startPos, double radius1, double radius2, double startAngle)
        {
            return GetPointInEllipse(startPos, radius1, radius2, startAngle + 180, backward);
        }

        PointD GetPointInEllipse(PointD center, double radius1, double radius2, double angleFromDown, bool backward)
        {
            double direct = backward ? -1 : 1;
            return new PointD(
                 center.X + radius1 * CosGrad(angleFromDown + 90)
                , center.Y + radius2 * SinGrad(angleFromDown + 90) * direct);
        }

        double SinGrad(double angleRad)
        {
            return Math.Sin((angleRad / 180.0) * Math.PI);
        }

        double CosGrad(double angleRad)
        {
            return Math.Cos((angleRad / 180.0) * Math.PI);
        }

        protected override void ExecuteBeforeStart()
        {
            posC = GetPosCenter(dancer.Pos, radius1, radius2, startAngle);
        }

        protected override void ExecuteAction(double actTimeS)
        {
            double ang = moveAngle * DoneValue;
            dancer.Pos = GetPointInEllipse(posC, radius1, radius2, startAngle + ang, backward);
            if (allwaysForward)
            {
                if (backward)
                    dancer.Angle = 360 - (startAngle + ang) - 90;
                else
                    dancer.Angle = startAngle + ang - 90;
            }
        }

        protected override void ExecuteDone()
        {
            dancer.Pos = GetPointInEllipse(posC, radius1, radius2, startAngle + moveAngle, backward);
            if (allwaysForward)
            {
                if (backward)
                    dancer.Angle = 360 - moveAngle + 180 + 90;
                else
                    dancer.Angle = moveAngle - 90;
            }
        }

    }

}
